package com.lej.club.wxapi

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lej.wx.BuildConfig
import com.lej.wx.WX_LOGIN
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelbiz.WXPayInsurance
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory

class WXEntryActivity : Activity(), IWXAPIEventHandler {
    private var api: IWXAPI? = null

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        api = WXAPIFactory.createWXAPI(this, BuildConfig.WX_APP_ID)
        api?.handleIntent(intent, this)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        api?.handleIntent(intent, this)
    }

    override fun onReq(req: BaseReq) {
    }

    override fun onResp(resp: BaseResp) {
        when (resp) {
            is SendAuth.Resp -> {
                when (resp.errCode) {
                    BaseResp.ErrCode.ERR_OK -> LiveEventBus.get<String>(WX_LOGIN).post(resp.code)
                    BaseResp.ErrCode.ERR_AUTH_DENIED -> Toast.makeText(this, "拒绝授权", Toast.LENGTH_SHORT)
                        .show()
                    BaseResp.ErrCode.ERR_USER_CANCEL -> Toast.makeText(this, "取消授权", Toast.LENGTH_SHORT)
                        .show()
                }
            }
            is WXPayInsurance.Resp -> {
            }
        }
        finish()
//        if (resp.type == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            LiveEventBus.get(WxResInfo::class.java).post(WxResInfo(resp.errCode, resp.errStr))
//        }
//        finish()
    }
}